//
//  HTTPManager.m
//  B_test
//
//  Created by 3g on 15/9/18.
//  Copyright (c) 2015年 chehao. All rights reserved.
//

#import "HTTPManager.h"
#import "ShowMessage.h"


@implementation HTTPManager

+ (instancetype)manager
{
    HTTPManager *mgr = [super manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];

    return mgr;
}

+ (void)get:(NSString *)url success:(void (^)(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject))success{
    NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];

    
    [[self manager] GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        [ShowMessage showMessage:@"与服务器连接出现错误，请检查网络"];

       
    }]; 
    
}


+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject))success{
    
    NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    
    [[self manager] POST:urlString parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        [ShowMessage showMessage:@"与服务器连接出现错误，请检查网络"];
    }];
}


+ (void)get:(NSString *)url success:(void (^)(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject))success failure:(void (^)(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error))failure{
    NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    
    
    [[self manager] GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        failure(operation, error);
        
        [ShowMessage showMessage:@"与服务器连接出现错误，请检查网络"];
        
    }];
    
}


+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject))success failure:(void (^)(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error))failure{
    
    NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    
    [[self manager] POST:urlString parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        failure(operation, error);
        NSLog ( @"operation: %@" , operation. responseString ); 
        [ShowMessage showMessage:@"与服务器连接出现错误，请检查网络"];
    }];
}


@end
